//
//  HomeViewController.m
//  BaoVn
//
//  Created by Le Anh Tai on 11/6/14.
//  Copyright (c) 2014 Le Anh Tai. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeWebSiteViewController.h"
#import "WebsiteCollectionView.h"
#import "HomeFlipView.h"
#import "RssFeedThread.h"
#include "SyncMutableArray.h"
#import "WebsiteInfo.h"
#import "SiteCollectionViewCell.h"

//#define __TEST__

@interface HomeViewController ()

@end

@implementation HomeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _websites = [[SyncMutableArray alloc] init];

#warning Read file/db/fake api
    [_websites addObject:[[WebsiteInfo alloc] initWithUri:@"http://vnexpress.net" rss:@"http://vnexpress.net/rss/tin-moi-nhat.rss"]];
    [_websites addObject:[[WebsiteInfo alloc] initWithUri:@"http://ngoisao.net" rss:@"http://ngoisao.net/rss/24h.rss"]];

//#ifdef __TEST__
//    _websiteCollectionView = [[WebsiteCollectionView alloc] init];
//    _websiteCollectionView.delegate = self;
//    [_contentView addSubview:_websiteCollectionView];
//#else
//    _flipView = [[HomeFlipView alloc] init];
//    _flipView.delegate = self;
//    
//    self.title = [NSString stringWithFormat:@"Home (1/%ld)", [_flipView getTotalPage]];
//    
//    [_contentView addSubview:_flipView];
//#endif
    
    _data = [[NSMutableArray alloc] init];
    
    //dummy data
    for (NSInteger i = 0; i < 100; i++)
    {
        NSString *urlImage;
        NSString *des;
        NSString *title;
        
        if (i % 4 == 3)
        {
            urlImage = @"https://lh5.ggpht.com/MZEFSBgwcY6x12AZq8buCsP3PBHDlkKm7PQDGvJr688Emz1GLbdfuQJ3RJzaJNni-A=h900";
            title = @"VNEXPRESS";
            des = @"Bao Tieng Viet nhieu nguoi xem nhat";
        }
        else if (i % 4 == 2)
        {
            urlImage = @"http://cdn.marketplaceimages.windowsphone.com/v8/images/19371155-4e41-4cf1-bcd4-a00bfec3a61a?imageType=ws_icon_large";
            title = @"GENK";
            des = @"Trang danh cho tin do cong nghe";
            
        }
        else if (i % 4 == 1)
        {
            urlImage = @"https://photo.tinhte.vn/store/avatars/l/730/730737.jpg?1394077624";
            title = @"TINH TE";
            des = @"Cong dong khoa hoc & cong nghe";
        }
        else
        {
            urlImage = @"https://lh6.googleusercontent.com/-LLIm_TOEbnU/AAAAAAAAAAI/AAAAAAAAAGA/JK4rtIhSroM/photo.jpg";
            title = @"TITLE";
            des = @"Some description about the site";
        }
        
        [_data addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                          urlImage, @"image",
                          title, TITLE_KEY,
                          des, DESCRIPTION_KEY,
                        nil]];
    }
            
    _size = 150;
    _padding = 8;
    
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.minimumLineSpacing = _padding;
    _layout.sectionInset = UIEdgeInsetsMake(_padding, _padding, _padding, _padding);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.pagingEnabled = YES;
    
    [_collectionView registerClass:[SiteCollectionViewCell class] forCellWithReuseIdentifier:@"SiteCollectionCell"];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [_contentView addSubview:_collectionView];
    
    _rssFeedThread = [[RssFeedThread alloc] initWithWebsites:_websites];
    [_rssFeedThread start];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
  
//#ifdef __TEST__
//    _websiteCollectionView.frame = _contentView.bounds;
//#else
//    _flipView.frame = _contentView.bounds;
//#endif
    
    _collectionView.frame = RECT(4, 4, _contentView.width - 8, _contentView.height - 8);
    _collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    
    _collumns = (_collectionView.width - _padding*2)/_size;
    _rows = (_collectionView.height - _padding*2)/(_size + 10);
    _layout.itemSize = SIZE((_collectionView.width - _padding*2 )/_collumns - _padding, (_collectionView.height - _padding*2)/_rows);
}

#pragma mark - Delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _data.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SiteCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SiteCollectionCell" forIndexPath:indexPath];
    
    [cell loadData:[_data objectAtIndex:indexPath.item]];
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    
    if (kind == UICollectionElementKindSectionHeader)
    {
        reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        
        int newItems = 3;
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:reusableview.bounds];
        
        [reusableview addSubview:scrollView];
        
        [scrollView setContentSize:SIZE(reusableview.width*newItems, reusableview.height)];
        [scrollView setShowsHorizontalScrollIndicator:NO];
        [scrollView setShowsVerticalScrollIndicator:NO];
        [scrollView setPagingEnabled:YES];
        
        int x = 0;
        int y = 0;
        int w = scrollView.width;
        int h = scrollView.height;
        
        for (int i = 0; i < newItems; i ++)
        {
            UIView *view = [[UIView alloc] initWithFrame:RECT(x, y, w, h)];
            
            UIColor *color = [UIColor grayColor];
            if (i == 0)
            {
                color = [UIColor redColor];
            }
            else if (i == 1)
            {
                color = [UIColor greenColor];
            }
            else if (i == 2)
            {
                color = [UIColor blueColor];
            }
            
            [view setBackgroundColor:color];
            
            x += w;
            
            [scrollView addSubview:view];
        }
    }
    
    return reusableview;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return SIZE(collectionView.width, _layout.itemSize.height);
}

//- (void)websiteCollectionViewDidTapWebsite:(NSInteger)websiteId
//{
//    HomeWebSiteViewController *controller = [[HomeWebSiteViewController alloc] init];
//    [self pushController:controller animated:YES];
//}
//
//- (void)websiteCollectionViewDidTapNew:(NSInteger)websiteId atNewsItem:(NSInteger)newsItemId
//{
//    HomeWebSiteViewController *controller = [[HomeWebSiteViewController alloc] init];
//    [self pushController:controller animated:YES];
//}
//
//- (void)flipView:(BaseFlipView *)view didSelectData:(NSDictionary *)data
//{
//    HomeWebSiteViewController *controller = [[HomeWebSiteViewController alloc] init];
//    [self pushController:controller animated:YES];
//}
//
//- (void)flipView:(BaseFlipView *)view didSelectPage:(NSInteger)pageNumber
//{
//    self.title = [NSString stringWithFormat:@"Home (%ld/%ld)", pageNumber, [_flipView getTotalPage]];
//}
@end
